package com.match.competition_sub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.match.competition_sub.entity.CompetitionDepartment;
import com.match.competition_sub.entity.department.OneSubject;
import com.match.competition_sub.entity.department.TwoSubject;
import com.match.competition_sub.mapper.CompetitionDepartmentMapper;
import com.match.competition_sub.service.CompetitionDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-21
 */
@Service
public class CompetitionDepartmentServiceImpl extends ServiceImpl<CompetitionDepartmentMapper, CompetitionDepartment> implements CompetitionDepartmentService {
    @Override
    public List<OneSubject> findAllDepartment() {
        //查询所有的系别
        QueryWrapper<CompetitionDepartment> departQuery=new QueryWrapper<>();
        departQuery.eq("parent_id","0");
        List<CompetitionDepartment> firstList = baseMapper.selectList(departQuery);

        //查询所有的专业
        QueryWrapper<CompetitionDepartment> majorQuery=new QueryWrapper<>();
        majorQuery.ne("parent_id","0");
        List<CompetitionDepartment> secondList = baseMapper.selectList(majorQuery);

        //封装最后返回的结果
        List<OneSubject> endList=new ArrayList<>();

        // 封装所有的一级(系别)
        for (int i = 0; i < firstList.size(); i++) {
            CompetitionDepartment leverObj = firstList.get(i);
            OneSubject oneSubject=new OneSubject();
            BeanUtils.copyProperties(leverObj,oneSubject);
            endList.add(oneSubject);
            //封装所有二级(专业)
            List<TwoSubject> twoSubjects=new ArrayList<>();
            // 遍历所有的二级目录
            for (int i1 = 0; i1 < secondList.size(); i1++) {
                CompetitionDepartment majorObj = secondList.get(i1);
                if (leverObj.getId().equals(majorObj.getParentId())){
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(majorObj,twoSubject);
                    twoSubjects.add(twoSubject);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            oneSubject.setChildren(twoSubjects);
        }
        return endList;
    }
}
